Queues

PDF versionPDF version

#include<stdio.h>       //header files
#include<conio.h>

void insertion();       //forward declaration
void deletion();
void display();

char q[20];              //global variables
char ele;
char ans;
int front=-1,rear=-1,ch,i;

void main()
{

 clrscr();
 do
 {
  clrscr();
  printf("\n\n What do you want to do? ");
  printf("\n\n 1 : INSERT");
  printf("\n 2 : DELETE");
  printf("\n 3 : DISPLAY");
  printf("\n 4 : EXIT");
  printf("\n\n Enter your choice here => ");
  scanf("%d",&ch);

  switch(ch)
  {
   case 1:{
    insertion();
    break;
   }
   case 2:{
    deletion();
    break;
   }
   case 3:{
    display();
    break;
   }
   case 4: exit();
   default: printf("\n\n You have entered a wrong choice.");
  }
  printf("\n\n Do you want to continue? (y/n): ");
  flushall();
  scanf("%c",&ans);
 }while(ans=='y');

 getch();
}

void insertion()
{
 if(front==rear)
  printf("\n\n Queue is empty.");
 printf("\n\n Enter the character to be inserted: ");
 flushall();
 scanf("%c",&ele);
 rear=rear+1;
 q[rear]=ele;
}

void deletion()
{
 front=front+1;
 q[front]=NULL;
 printf("\n\n Character %c has been deleted. ",q[front]);
 if(front==rear)
  printf("\n\n Queue is empty.");

}

void display()
{
 i=front;

 if(front==rear)
  printf("\n\n Queue is empty.");
 front=front+1;
 while(front<=rear)
 {
  printf("%c->",q[front]);
  front=front+1;
 }

 front=i;
}